<?php
/**
 * 新闻咨询新增修改
 * User: hgq
 * Date: 2016/7/8 0008
 * Time: 下午 16:56
 */
use yii\helpers\Url;

$web = Url::base();

?>
<!-- 配置文件 -->
<script type="text/javascript" src="<?= $web ?>/components/ueditor/ueditor.config.js"></script>
<!-- 编辑器源码文件 -->
<script type="text/javascript" src="<?= $web ?>/components/ueditor/ueditor.all.js"></script>
<script type="text/javascript" src="<?= $web ?>/components/ueditor/lang/zh-cn/zh-cn.js"></script>
<link type="text/css" href="<?= $web ?>/components/ueditor/themes/default/css/ueditor.css" rel="stylesheet"/>
<link type="text/css" href="<?= $web ?>/components/ueditor/themes/iframe.css" rel="stylesheet"/>

<script type="application/javascript">
    // 实例化编辑器
    var editor = UE.getEditor('edit_content_html');

    $(function ($) {
        var detail = <?=json_encode($detail)?>;
        if (detail != null) {
            var content_html = detail.content_html;
            editor.addListener("ready", function () {
                // editor准备好之后才可以使用
                editor.setContent(content_html);
            });
        }
    });

    //返回
    function goback() {
        var institution_code = $("#institution_code").val();
        var page = $("#page").val();
        var title = $("#title").val();
        var is_shown = $("#is_shown").val();

        var urlstr = "news";
        if (page == null || page == '') {
            page = 1;
        }
        if (institution_code == null || institution_code == '') {
            urlstr = urlstr = '0';
        }
        urlstr = setUrlParam(urlstr, 'page', page);
        urlstr = setUrlParam(urlstr, 'inst', institution_code);
        if (title != null && title != '') {
            urlstr = setUrlParam(urlstr, 'title', title);
        }
        if (is_shown != null && is_shown != '') {
            urlstr = setUrlParam(urlstr, 'is_shown', is_shown);
        }

        window.location.href = urlstr;
        loading.show();
    }
    //继续新增
    function continueAddData() {
        $("#edit_id").val('');
        $("#edit_title").val('');
        $("#edit_category_id").val('');
        $("#edit_content_html").val('');
        $("#edit_description").val('');
        $("#edit_is_shown").val('1');
    }

    //数据保存
    function saveData() {
        var id = $("#edit_id").val();
        var institution_code = $("#edit_institution_code").val();
        var title = $("#edit_title").val();
        var category_id = $("#edit_category_id").val();
//        var content_html = $("#edit_content_html").val();
        var description = $("#edit_description").val();
        var is_shown = $("#edit_is_shown").val();
        var content_html = editor.getContent();
        if (title == null || title == '') {
            $("#hint_message").text('咨询标题不能为空！');
            $("#hintMessageModal").modal('show');
        } else if (category_id == null || category_id == '') {
            $("#hint_message").text('所属分类不能为空！');
            $("#hintMessageModal").modal('show');
        } else {
            var data = {};
            data.id = id;
            data.institution_code = institution_code;
            data.title = title;
            data.category_id = category_id;
            data.content_html = content_html;
            data.description = description;
            data.is_shown = is_shown;
            requestJson("news-save-handler", data, true, function (response) {
                if (response.status == 0) {
                    var rtid = response.data.id;
                    $("#edit_id").val(rtid);
                    $("#hint_message").text('操作成功！');
                } else {
                    $("#hint_message").text('操作失败！');
                }
                $("#hintMessageModal").modal('show');
            });
        }
    }

</script>
<!-- head filter-->
<div class="panel panel-default">
    <div class="panel-body">
        <div class="form-inline">
            <div class="btn-group">
                <button class="btn btn-primary" type="button" onclick="goback()">
                    <i class="fa fa-reply"></i>返回
                </button><?php
                if ($id == null || $id == '') {
                    ?>
                    <button type="button" id="continueAdd" class="btn btn-primary" onclick="continueAddData()"><i
                            class="fa fa-repeat"></i> 继续新增
                    </button>
                    <?php
                }
                ?>
                <button type="button" class="btn btn-primary" onclick="saveData()"><i class="fa fa-save"></i> 保存
                </button>
            </div>
        </div>
    </div>
</div>
<div class="panel panel-default">
    <div class="panel-body">
        <!-- 保留参数字段 -->
        <input type="hidden" id="institution_code" class="form-control" value="<?= $institution_code ?>"
               readOnly="true">
        <input type="hidden" id="page" class="form-control" value="<?= $page ?>" readOnly="true">
        <input type="hidden" id="title" class="form-control" value="<?= $title ?>" readOnly="true">
        <input type="hidden" id="is_shown" class="form-control" value="<?= $is_shown ?>" readOnly="true">
        <!-- 保留参数字段 -->
        <?php
        if ($detail != '') {
            ?>
            <form class="form-horizontal adminex-form">
                <div class="form-group">
                    <input type="hidden" id="edit_id" class="form-control" value="<?= $detail["id"] ?>" readOnly="true">
                    <input type="hidden" id="edit_institution_code" class="form-control"
                           value="<?= $detail["institution_code"] ?>" readOnly="true">

                    <label class="col-sm-2 control-label">咨询标题：</label>
                    <div class="col-sm-4">
                        <input type="text" id="edit_title" class="form-control" style=" max-width: 260px " value="<?= $detail["title"] ?>">
                    </div>
                    <label class="col-sm-2 control-label">所属分类：</label>
                    <div class="col-sm-4">
                        <select id="edit_category_id" class="form-control" style=" min-width: 200px; max-width: 260px " onchange="">
                            <?php
                            if ($category != null && $category != '') {
                                for ($i = 0; $i < count($category); $i++) {
                                    ?>
                                    <option
                                        value="<?= $category[$i]['id'] ?>" <?php if ($detail["category_id"] == $category[$i]['id']) echo "selected=\"selected\""; ?>><?= $category[$i]['name'] ?></option>
                                    <?php
                                }
                            }
                            ?>
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">咨询描述：</label>
                    <div class="col-sm-10">
                        <textarea id="edit_description" class="form-control" style="width: 716px;"
                                  rows="3"><?= $detail["description"] ?></textarea>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">是否显示：</label>
                    <div class="col-sm-4">
                        <select id="edit_is_shown" class="form-control" style=" min-width: 200px; max-width: 260px " onchange="">
                            <option value="1" <?php if ($detail["is_shown"] == "1") echo "selected=\"selected\""; ?>>显示
                            </option>
                            <option value="0" <?php if ($detail["is_shown"] == "0") echo "selected=\"selected\""; ?>>不显示
                            </option>
                        </select>
                    </div>
                    <label class="col-sm-2 control-label"></label>
                    <div class="col-sm-4">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">咨询内容：</label>
                    <div class="col-sm-9">
                        <div>
                            <!-- 加载编辑器的容器 -->
                            <script id="edit_content_html" name="content" type="text/plain"
                                    style="width:716px;height:400px;"></script>
                        </div>
                    </div>
                </div>
            </form>
            <?php
        } else {
            ?>
            <form class="form-horizontal adminex-form">
                <div class="form-group">
                    <input type="hidden" id="edit_id" class="form-control" value="" readOnly="true">
                    <input type="hidden" id="edit_institution_code" class="form-control"
                           value="<?= $institution_code ?>" readOnly="true">

                    <label class="col-sm-2 control-label">咨询标题：</label>
                    <div class="col-sm-4">
                        <input type="text" id="edit_title" class="form-control" style=" max-width: 260px " value="">
                    </div>
                    <label class="col-sm-2 control-label">所属分类：</label>
                    <div class="col-sm-4">
                        <select id="edit_category_id" class="form-control" style=" min-width: 200px; max-width: 260px "
                                onchange="">
                            <?php
                            if ($category != null && $category != '') {
                                for ($i = 0; $i < count($category); $i++) {
                                    ?>
                                    <option
                                        value="<?= $category[$i]['id'] ?>"><?= $category[$i]['name'] ?></option>
                                    <?php
                                }
                            }
                            ?>
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">咨询描述：</label>
                    <div class="col-sm-10">
                        <textarea id="edit_description" class="form-control" style="width: 716px;" rows="3"></textarea>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">是否显示：</label>
                    <div class="col-sm-4">
                        <select id="edit_is_shown" class="form-control" style=" min-width: 200px; max-width: 260px "
                                onchange="">
                            <option value="1" selected="selected">显示</option>
                            <option value="0">不显示</option>
                        </select>
                    </div>
                    <label class="col-sm-2 control-label"></label>
                    <div class="col-sm-4">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">咨询内容：</label>
                    <div class="col-sm-9">
                        <div>
                            <!-- 加载编辑器的容器 -->
                            <script id="edit_content_html" name="content" type="text/plain"
                                    style="width:716px;height:400px;"></script>

                        </div>
                    </div>
                </div>
            </form>
            <?php
        }
        ?>
    </div>
</div>
<!--提示对话框-->
<div aria-hidden="true" role="dialog" tabindex="-1" id="hintMessageModal" class="modal fade">
    <div class="modal-dialog modal-sms">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                <h4 class="modal-title">提示</h4>
            </div>
            <div class="modal-body" id="hint_message" align="center">
            </div>
        </div>
    </div>
</div>